/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minValue;

import java.util.ArrayList;

/**
 *
 * @author mj
 */
class Aera {

    private ArrayList<Linex> alx1;
    private ArrayList<Linex> alx2;

    Aera(double[] d1, double[] d2) throws Exception {
        double[] line1 = d1.clone();
        double[] line2 = d2.clone();
        if (line1.length != line2.length) {
            throw (new Exception());
        }
        alx1 = prehandle(line1);
        alx2 = prehandle(line2);
    }

    private ArrayList<Linex> prehandle(double[] line1) {
        for (int i = 0; i < line1.length; i++) {
            double d = line1[i];
            line1[i] = d * 100;
        }

        ArrayList<Point> apt = new ArrayList<Point>();
        for (int i = 0; i < line1.length; i++) {
            Point np = new Point(i * 1.3, line1[i]);
            apt.add(np);
        }

        ArrayList<Linex> alx = new ArrayList<Linex>();
        for (int i = 0; i < apt.size() - 1; i++) {
            Point first = apt.get(i);
            Point second = apt.get(i + 1);
            Linex lx = new Linex(first, second);
            alx.add(lx);
        }
        return alx;
    }

    arearesult computeArea() {
        double sum = 0;
        int correctsum = 0;
        for (int i = 0; i < alx1.size(); i++) {
            Linex lx1 = alx1.get(i);
            Linex lx2 = alx2.get(i);
            if (compareTwoLine(lx1, lx2)) {
                correctsum++;
            }
            sum = sum + arealx(lx1, lx2);
        }

        arearesult arsult = new arearesult(correctsum, sum);
        return arsult;
    }

    private double arealx(Linex lx1, Linex lx2) {
        try {
            lx1.comparelx(lx2);
        } catch (ZeroException ex) {
            return 0;
        } catch (IntersectException ex) {
            return InSection(lx1, lx2);
        } catch (TripeException e) {
            return Triple(lx1, lx2);
        }
        return trapezium(lx1, lx2);

    }

    private double trapezium(Linex lx1, Linex lx2) {

        return lx1.trapezium(lx2);
    }

    private double InSection(Linex lx1, Linex lx2) {
        return lx1.interaction(lx2);
    }

    private double Triple(Linex lx1, Linex lx2) {
        return lx1.Triple(lx2);
    }

    private boolean compareTwoLine(Linex lx1, Linex lx2) {
        return lx1.compareTwoLine(lx2);

    }

    int computeBegCorrect() {
        int correctsum = 0;
        for (int i = 0; i < alx1.size(); i++) {
            Linex lx1 = alx1.get(i);
            Linex lx2 = alx2.get(i);
            if (compareTwoLine(lx1, lx2)) {
                correctsum++;
            } else {
                break;
            }
        }
        return correctsum;
    }
}
